<template>
    <el-dialog :visible.sync="loginDialogVisible" style="width:800px; margin: 20px auto;">
        <h3 class="title">系统登录</h3>
        <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="0px"
                 style="width: 90%; margin: 0px auto">
            <!--class="demo-ruleForm login-container">-->
            <el-form-item prop="account">
                <el-input type="text" v-model="ruleForm2.account" auto-complete="on" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
                <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="on" placeholder="密码"></el-input>
            </el-form-item>
            <!--<el-checkbox v-model="checked"  class="remember">记住密码</el-checkbox>-->
            <el-form-item>
                <el-button type="primary" style="width:47%; float: left" @click="cancle">取消</el-button>
                <el-button type="primary" style="width:47%; float:right" @click.native.prevent="login"
                           :loading="logining">确定
                </el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
    import {requestLogin} from '../api/user';
    import md5 from 'js-md5'

    export default {
        name: "loginDialog",
        data() {
            loginDialogVisible:false;
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPassword !== '') {
                        this.$refs.form.validateField('checkPassword');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                loginDialogVisible: false,
                logining: false,
                ruleForm2: {
                    account: '',
                    checkPass: ''
                },
                form: {
                    name: '',
                    password: '',
                    checkPassword: '',
                    // role:''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPassword: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                rules2: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    checkPass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
                // checked: false
            };
        },
        methods: {
            cancle() {
                this.$refs.ruleForm2.resetFields();
                this.loginDialogVisible = false;
                this.logining = false;
            },
            login() {
                this.$refs.ruleForm2.validate((valid) => {
                    if (valid) {
                        this.logining = true;
                        let loginParams = {name: this.ruleForm2.account, password: md5(this.ruleForm2.checkPass)};
                        let that = this;
                        this.$axios.post(that.$URL.userLogin, loginParams).then(
                            function (res) {
                                if (res.code == 200) {
                                    that.$message({
                                        message: "用户登陆成功",
                                        type: 'success'
                                    });
                                    that.$refs.ruleForm2.resetFields();
                                    that.logining = false;
                                    that.loginDialogVisible=false;
                                    sessionStorage.setItem('user', JSON.stringify(res.body));
                                    that.$router.push({path: '/map'});

                                }
                                else if(res.code==1001){
                                    that.$message({
                                        message: "密码错误",
                                        type: 'warning'
                                    });
                                    that.logining = false;
                                }else if(res.code==201){
                                    that.$message({
                                        message: "管理员登陆成功",
                                        type: 'success'
                                    });
                                    that.$refs.ruleForm2.resetFields();
                                    that.logining = false;
                                    that.loginDialogVisible=false;
                                    sessionStorage.setItem('user', JSON.stringify(res.body));
                                    that.$router.push({path: '/manager'});
                                }
                            }
                        );
                        // requestLogin(loginParams).then(res => {
                        //     this.logining = false;
                        //     let {message, body} = res.data;
                        //     if (res.status !== 200) {
                        //         this.$message({
                        //             message: message,
                        //             type: 'error'
                        //         });
                        //     } else {
                        //         sessionStorage.setItem('user', JSON.stringify(body));
                        //         this.$router.push({path: '/map'});
                        //     }
                        // });
                    }
                });
            },
        },
        created() {
            let that = this;
            this.$Bus.$on('showLogin', res => {
                that.loginDialogVisible = res;
            })
        }

    }
</script>

<style>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }

    .remember {
        margin: 0px 0px 35px 0px;
    }

    .dialogTitle {
        margin: 0px auto 30px auto;
        text-align: center;
        color: #505458;
    }

    .el-form-item {
        margin-bottom: 18px;
    }
</style>